package rpc.core;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import rpc.trace.TraceContext;


/*********
 *
 *
 *@author 197
 *@date 2020/8/3 10:36
 *
 **/


public class FlowReq implements java.io.Serializable {

  protected FlowMap data = new FlowMap();
  private long timeOut = 3000;
  private RetryConfig retryConfig = new RetryConfig();

  private TraceContext traceContext;

  public FlowReq() {
  }

  public FlowReq(Map<String, Object> map) {
    if (Objects.nonNull(map)) {
      map.forEach((k, v) -> data.append(k, v));
    }
  }

  public FlowReq setTraceContext(TraceContext traceContext) {
    this.traceContext = traceContext;
    return this;
  }

  public FlowReq append(String key, Object value) {
    data.append(key, value);
    return this;
  }

  public Object o(String key) {
    return data.o(key);
  }

  public List<FlowMap> nlm(String key) {
    return data.nlm(key);
  }

  public String ns(String key) {
    return data.ns(key);
  }

  public long l(String key) {
    return this.data.l(key);
  }

  public Map<String, Object> toMap() {
    return this.data.toMap();
  }

  public List<String> ls(String key) {
    return this.data.nls(key);
  }

  public long l(String key, long dv) {

    return this.data.containsKey(key) ? this.data.l(key) : dv;
  }

  public Map<String, Object> nm(String key) {
    return this.data.nm(key);
  }

  public long getTimeOut() {
    return timeOut;
  }

  public FlowReq setTimeOut(long timeOut) {
    this.timeOut = timeOut;
    return this;
  }

  /***
   *  delayTimes 重试间隔
   * @param delayTimes 默认间隔 1秒
   * @return
   */
  public FlowReq setDelayTimes(Integer... delayTimes) {
    getRetryConfig().setDelayTimes(delayTimes);
    return this;
  }

  public RetryConfig getRetryConfig() {
    return retryConfig;
  }

  public FlowReq setRetryConfig(RetryConfig retryConfig) {
    this.retryConfig = retryConfig;
    return this;
  }

  /***
   *   最大重试次数
   * @param maxCount  -2  无限次， 以 retCode=0  视为ok
   *
   * @return
   */
  public FlowReq setMaxCount(int maxCount) {
    getRetryConfig().setMaxCount(maxCount);
    return this;
  }
}
